package com.rd.student.jiaji.class1.jinhongyu.day0801;

import java.util.Scanner;

public class Matrix {

    public static void main(String[] args) {
        juzhen();
    }

    public static void juzhen() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入矩阵大小");
        int n = scanner.nextInt();
        int[][] nn = new int[n][n];
        int Start = 0;//定值
        int End = n - 1;//末尾下标
        int z = 1;
        int m = n;
        if (m % 2 == 0 && m > 0) {//偶数矩阵： 2 4 6 8 10 (m不为0且大于0)
            for (int i = 0; i < m / 2; i++) {//环数
                //向右
                for (int a = Start; a < End; a++) {
                    nn[Start][a] = z;
                    z++;
                }
                //向下
                for (int b = Start; b < End; b++) {
                    nn[b][End] = z;
                    z++;
                }
                //向左
                for (int c = End; c > Start; c--) {
                    nn[End][c] = z;
                    z++;
                }
                //向右
                for (int d = End; d > Start; d--) {
                    nn[d][Start] = z;
                    z++;
                }
                Start++;
                End--;
            }
        } else if (m % 2 == 1 && m > 0) {//单数矩阵1 3 5 7 9 m大于0
            for (int i = 0; i < m/ 2; i++) { // 环数
                // 向右
                for (int a = Start; a <= End; a++) {
                    nn[Start][a] = z;
                    z++;
                }
                // 向下
                for (int b = Start + 1; b <= End; b++) {
                    nn[b][End] = z;
                    z++;
                }
                // 向左
                for (int c = End - 1; c >= Start; c--) {
                    nn[End][c] = z;
                    z++;
                }
                // 向上
                for (int d = End - 1; d > Start; d--) {
                    nn[d][Start] = z;
                    z++;
                }
                Start++;
                End--;
            }
            nn[m / 2][m / 2] = z;
        }

        for (int m1 = 0; m1 < nn.length; m1++) {
            for (int n1 = 0; n1 < nn[m1].length; n1++) {
//                System.out.print(String.format("%3d", arr[m][n]) + "\t");
                System.out.print(nn[m1][n1] + "\t");
            }
            System.out.println();
        }
    }
}
